package edu.ncsu.csc.itrust.dao.officevisit;

import java.util.List;
import junit.framework.TestCase;
import edu.ncsu.csc.itrust.action.EditPrescriptionAction;
import edu.ncsu.csc.itrust.beans.MedicationBean;
import edu.ncsu.csc.itrust.beans.OverrideReasonBean;
import edu.ncsu.csc.itrust.beans.PrescriptionBean;
import edu.ncsu.csc.itrust.dao.DAOFactory;
import edu.ncsu.csc.itrust.dao.mysql.OfficeVisitDAO;
import edu.ncsu.csc.itrust.dao.mysql.OfficeVisitMedicationDAO;
import edu.ncsu.csc.itrust.datagenerators.TestDataGenerator;
import edu.ncsu.csc.itrust.testutils.TestDAOFactory;

public class OVMedicationsTest extends TestCase {
	private OfficeVisitMedicationDAO ovmDAO = TestDAOFactory.getTestInstance().getOVMedicationDAO();
	private PrescriptionBean pres;
	
	@Override
	protected void setUp() throws Exception {
		TestDataGenerator gen = new TestDataGenerator();
		gen.clearAllTables();
		gen.ndCodes();
		gen.officeVisit1();
		gen.ORCodes();
		pres = new PrescriptionBean();
		MedicationBean medication = new MedicationBean();
		medication.setDescription("Tetracycline");
		medication.setNDCode("009042407");
		pres.setMedication(medication );
		pres.setDosage(50);
		pres.setStartDateStr("05/19/2007");
		pres.setEndDateStr("05/19/2010");
		pres.setVisitID(1L);
		pres.setInstructions("Take twice daily");
		
	}

	public void testAddRemoveOneOVPrescription() throws Exception {
		assertEquals("no current prescriptions on office vist 1", 0, ovmDAO.getPrescriptions(1).size());
		long ovMedID = ovmDAO.addPrescription(pres);
		List<PrescriptionBean> meds = ovmDAO.getPrescriptions(1);
		assertEquals("now there's 1", 1, meds.size());
		assertEquals(pres, meds.get(0));
		ovmDAO.removePrescription(ovMedID);
		assertEquals("now there's none", 0, ovmDAO.getPrescriptions(1).size());
	}

	public void testAddMultipleRemoveSingle() throws Exception {
		assertEquals("no current prescriptions on office vist 1", 0, ovmDAO.getPrescriptions(1).size());
		long ovMedID = ovmDAO.addPrescription(pres);
		ovmDAO.addPrescription(pres);
		List<PrescriptionBean> meds = ovmDAO.getPrescriptions(1);
		assertEquals("now there's 2", 2, meds.size());
		assertEquals(pres, meds.get(0));
		ovmDAO.removePrescription(ovMedID);
		assertEquals("now there's one", 1, ovmDAO.getPrescriptions(1).size());
	}

	public void testRemoveNonExistant() throws Exception {
		assertEquals("no current prescriptions on office vist 1", 0, ovmDAO.getPrescriptions(1).size());
		ovmDAO.removePrescription(50L);
		assertEquals("no current prescriptions on office vist 1", 0, ovmDAO.getPrescriptions(1).size());
	}
	
	public void testAddGetEditOverRide() throws Exception {
		setUp();
		
		long ovMedID = ovmDAO.addPrescription(pres);
		OverrideReasonBean orb = new OverrideReasonBean("00001", "Oogah boogah boo");
		orb.setPresID(ovMedID);
		ovmDAO.addReason(orb);
		List<OverrideReasonBean> bean = ovmDAO.getOverrideReasons(ovMedID);
		assertEquals( "00001" , bean.get(0).getORCode() );
		OverrideReasonBean orb2 = new OverrideReasonBean("00001", "Day[9] is awesome");
		orb2.setPresID(ovMedID);
		ovmDAO.editOverrideReason(orb2);
		bean = ovmDAO.getOverrideReasons(ovMedID);
		assertEquals("00001" , bean.get(0).getORCode() );
		assertEquals("Day[9] is awesome" , bean.get(0).getDescription());
		
	}

}
